﻿using UnityEngine;
using System.Collections;

public class CandyController : MonoBehaviour
{
	[SerializeField]
	private GameObject candy_holder;
	private GameObject candy_object;
    private GameObject neighbor_left, neighbor_right, neighbor_up, neighbor_down;
	private string[]obj_type={"red","green","purple","blue","yellow","orange"};
	private string color;
	private string type="simple";
	private Candy c;
    private GameObject candy;
	[SerializeField]
	private bool is_match=false;
	private bool isTouch;
	public bool is_fall_down;
	public bool check;
	private GameManager manager=GameManager.getInstance();

	public GameObject CandyObject{
		set{candy_object=value;}
		get{return candy_object;}
	}
	public bool IsMatched{
		set{is_match=value;}
		get{return is_match;}
	}
	public void CreateCandy(){
		color=obj_type[Random.Range(0,obj_type.Length)];	
		Destroy(candy_object);
		if(c==null){c=new Candy();}
		candy_object=c.getCandy(color,type).GetCandy();
		candy_object.transform.parent=candy_holder.transform;
		Destroy(candy_object.GetComponent<BoxCollider>());
		candy_object.transform.localPosition=Vector2.zero;
	}
	public GameObject CandyHolder{
		get{return candy_holder;}
	}
	public void Start(){
		c=new Candy();
		CreateCandy();
		manager=GameManager.getInstance();
	}
	public string Type{
		set{type=value;}
		get{return type;}
	}
	public string Candy_Color{
		get{return color;}
        set { color = value; }
	}
	public int YCoord{
		get{return Mathf.RoundToInt(this.transform.position.y);}
	}
	public int XCoord{
		get{return Mathf.RoundToInt(this.transform.position.x);}
	}
    public void AddNeightbor(GameObject other, string dir)
    {
		if(other==null){return;}
		switch(dir){
		case "up": neighbor_up=other;break;
		case "right":neighbor_right=other;break;
		case "down":neighbor_down=other;break;
		case "left":neighbor_left=other;break;
		}
	}
    public void RemoveNeighbor(string dir)
    {
		switch(dir){
		case "up": neighbor_up=null;break;
		case "right":neighbor_right=null;break;
		case "down":neighbor_down=null;break;
		case "left":neighbor_left=null;break;
		}
	}
	GameObject obj=null;
	public GameObject GetNeighbor(string dir){	
		switch(dir){
		case "up": obj= neighbor_up;break;
		case "right":obj= neighbor_right;break;
		case "down":obj= neighbor_down;break;
		case "left":obj= neighbor_left;break;
		}
		return obj;
	}
	public Move GetDirectionNeighbor(GameObject other){
		if(this.GetNeighbor("up")==other){
			return Move.up;
		}else if(this.GetNeighbor("down")==other){
			return Move.down;
		}else if(this.GetNeighbor("right")==other){
			return Move.right;
		}else if(this.GetNeighbor("left")==other){
			return Move.left;
		}
		return Move.none;
	}
	public bool IsNeithborWith(GameObject other){
		if(neighbor_up.Equals(other)){
			return true;
		}else if(neighbor_down.Equals(other)){
			return true;
		}else if(neighbor_left.Equals(other)){
			return true;
		}else if(neighbor_right.Equals(other)){
			return true;
		}
		return false;
	}
	public void ClearNeighbor(){
		RemoveNeighbor("up");
		RemoveNeighbor("down");
		RemoveNeighbor("left");
		RemoveNeighbor("right");
	}
//	public bool FallDown(){
//		this.is_fall_down=true;
//		if((this.GetNeighbor("up")==null)||(this==this.GetNeighbor("up"))){return false;}
//		return this.GetNeighbor("up").FallDown();
//	}
	void OnMouseDown(){
        manager.IsShow = false;
		isTouch=true;
		foreach(GameObject cs in GameObject.FindWithTag("candycrush").GetComponent<CandyDemo>().level.GetCandy()){
			cs.GetComponent<CandyController>().is_fall_down=false;
			cs.GetComponent<CandyController>().check=false;
		}
		if(manager.gamestate==GameState.playing){
        	candy = this.gameObject;
			GameObject.FindWithTag("candycrush").GetComponent<CandyDemo>().Hint();
			
			GameObject.FindWithTag("candycrush").GetComponent<CandyDemo>().Hint();
		}
        if (manager.NoMove) {
            CandyCheckMatch.instance.NoMoreMatch();
            manager.NoMove = false;
        }
        
	}
    void OnMouseUp()
    { isTouch=false; }
	void OnMouseDrag()
	{
		if (!isTouch){return;}		
		Vector3 tempPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		
		if (Vector2.Distance(transform.position, tempPosition) >  1f)
		{
			if (Mathf.Abs(tempPosition.x - transform.position.x) > Mathf.Abs(tempPosition.y - transform.position.y)) //left right moved
			{
				if (tempPosition.x > transform.position.x)
				{
					manager.Direction=Move.right;
                    FindTag();
				}
				else
				{
					manager.Direction=Move.left;
                    FindTag();
				}
			}
			else
			{
				if (tempPosition.y > transform.position.y)
				{
					manager.Direction=Move.up;
                    FindTag();
				}
				else
				{
					manager.Direction=Move.down;
					if (manager.gamestate == GameState.playing)
					{manager.gamestate=GameState.none;
                    GameObject.FindWithTag("candycrush").GetComponent<CandyMovement>().SwapCandy(candy);
					}
				}
			}
			OnMouseUp();
		}
	}
    private void FindTag()
    {
        if (manager.gamestate == GameState.playing)
        {
			manager.gamestate=GameState.moving;
			isTouch=false;
            GameObject.FindWithTag("candycrush").GetComponent<CandyMovement>().SwapCandy(candy);
        }
    }
}